<template>
	<view style="height: 100vh;"
		:style="{background:'url('+Imgbase+'/brave_goodpoor_bgc.png)','background-size':'100% 100%',}">
		<view class="padding-top-70 padding-left-40 flex-between">
			<view class="">
				<image class="w-80 h-80" :src="Imgbase + '/brave_goodindex_img1.png'" mode="aspectFit" @click="back">
				</image>

			</view>
		</view>


		<view class="w100 text-center padding-top-50">
			<image class="w100 h-750" :src="Imgbase + '/brave_goodlogin_img1.png'" mode="aspectFit"></image>
		</view>

		<view class="w100 text-center padding-top-30 font-weight-600 black-color">
			申请获取以下权限
			<view class="">
				获得您的公开信息(昵称、头像等)
			</view>
		</view>
		<view class="text-center margin-top-100">
			<image style="width: 650rpx;height: 120rpx;" :src="Imgbase + '/brave_goodlogin_img3.png'" @click="login"
				mode="aspectFit"></image>
		</view>

		<view class="cu-modal" :class="modalName=='getPhoneNumber'?'show':''">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view class="content">认证手机号</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class="padding-xl bg-white">
					您还未认证手机号，为了程序功能的正常使用请您进行微信手机认证，否则将无法通过APP手机登录到该账号。
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-blue text-blue" @click="quxs" @tap="hideModal">取消</button>
						<button class="cu-btn bg-blue margin-left" open-type="getPhoneNumber"
							@getphonenumber="onGetPhoneNumber">确定</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import global from '@/apiPost/mixin.js'
	export default {
		data() {
			return {
				Imgbase: getApp().globalData.Imgbase,
				urls: '',
				id: 0,
				phone: 0,
				money: 0,
				modalName: null,
				token: '',
				name: ''
			}
		},
		onLoad(e) {
			if (e.id) {
				this.id = e.id
				this.phone = e.phone
				this.money = e.money
			}
			if (e.name) {
				this.name = e.name
				console.log(this.name)
			}
		},
		methods: {
			quxs() {
				this.modalName = null
			},
			back() {
				if (this.id != 0) {
					uni.navigateTo({
						url: '/pages/goodspoor/invest'
					})
				} else {
					uni.navigateBack()
				}
			},
			onGetPhoneNumber(e) {
				let _this = this;
				wx.login({
					success(res) {
						uni.request({
							url: _this.$globals.apibase + '/api/Minlogin/getPhoneNumber',
							data: {
								token: _this.token,
								code: res.code,
								encryptedData: e.detail.encryptedData,
								iv: e.detail.iv,
								ver: _this.$mpVer
							},
							method: 'POST',
							header: {
								'content-type': 'application/json'
							},
							success: (res) => {
								if (res.data.code == '1') {
									uni.setStorageSync(
										'token',
										_this.token);
									uni.showToast({
										icon: 'none',
										title: '授权成功!',
										duration: 2000,
									});
									setTimeout(() => {
										uni.navigateBack()
									}, 2000)
								} else {
									uni.showToast({
										icon: 'none',
										title: '授权失败!',
										duration: 3000,
									});
								}
								_this.modalName = null
							}
						});
					}
				})
			},
			havePhoneNumber(token) {
				uni.setStorageSync(
					'token',
					token);
				uni.navigateBack()
				return
				let _this = this;
				this.token = token
				uni.request({
					url: _this.$globals.apibase + '/api/Minlogin/havePhoneNumber',
					data: {
						token: token,
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data.code == '401') {
							uni.setStorageSync('token', '');
							//_this.$uniApi.trylogin();
						}
						if (res.data.code == '1') {
							_this.phonestate = res.data.data.state;
							if (_this.phonestate == 0) {
								_this.modalName = 'getPhoneNumber';
							} else {
								uni.setStorageSync(
									'token',
									token);
								uni.showToast({
									icon: 'none',
									title: '登录成功',
									duration: 2000,
								});
								uni.setStorageSync(
									'session_key',
									true)
								_this.global.methods.getSkin()
								setTimeout(() => {
									uni.navigateBack()
									// if(_this.name == 'name'){
									// 	console.log(123123123123123)
									// }else{
									// 	uni.switchTab({
									// 		url:'/pages/index/index?mes=' + _this.name
									// 	})
									// }
								}, 1200)
							}
						}
					}
				});
			},
			async getTestToken() {
				let item = await this.$api.post('Minlogin/getTestToken', {
					user_id: 5673	
				})
				uni.setStorageSync('token', item.data.token);

			},
			login() {
				// this.getTestToken()
				// setTimeout(() => {
				// 	uni.navigateBack()
				// }, 500)
				// return

				if (this.id != 0) {}
				var _this = this;
				wx.showModal({
					title: '温馨提示',
					content: '正在请求您的个人信息',
					success(res) {
						if (res.confirm) {
							wx.getUserProfile({
								desc: "获取你的昵称、头像、地区及性别",
								success: function(res) {
									uni.showLoading({
										title: '登录中...'
									});
									_this.urls = _this.$globals.apibase + '/api/Minlogin/login'
									uni.login({
										provider: 'weixin',
										success: function(loginRes) {
											let code = loginRes.code;
											
											// console.log(code)
											// return
											uni.request({
												url: _this.$globals.apibase +
													'/api/Minlogin/login',
												data: {
													code: code,
													avatar: '',
													nickname: res.userInfo
														.nickName,
													gender: res.userInfo.gender,
													encryptedData: res
														.encryptedData,
													iv: res.iv,
													ver: _this.$mpVer,
													id: _this.id,
													phone: _this.phone,
												},
												method: 'POST',
												header: {
													'content-type': 'application/json'
												},
												success: (res) => {
													if (res.data.code == '1') {


														// console.log(123123123)
														uni.hideLoading();
														uni.setStorageSync(
															'token',
															res
															.data.data
															.token);
														uni.setStorageSync(
															'session_key',
															true)
														uni.showToast({
															icon: 'none',
															title: '登录成功',
															duration: 2000,
														});
														_this.global.methods
															.getSkin()
														setTimeout(() => {
															uni.navigateBack()
														}, 1200)

														// if (_this.money > 0) {
														// 	uni.setStorageSync(
														// 		'token',
														// 		res
														// 		.data.data
														// 		.token);
														// 	uni.showToast({
														// 		icon: 'none',
														// 		title: '登录成功'
														// 	});
														// 	setTimeout(() => {

														// 		uni.navigateTo({
														// 			url: '/pages/goodspoor/invest?id=' +
														// 				_this
														// 				.money
														// 		})
														// 	}, 1000)
														// } else {
														// 	_this
														// 		.havePhoneNumber(
														// 			res
														// 			.data.data
														// 			.token)
														// }
													}
												}
											});
										},
									});
								}
							})
						}
					}
				})
			}
		},
		onUnload() {

		},
	}
</script>

<style lang="scss">
	page {
		background-color: #F9F3EC;
	}
</style>